
classdef SimpleMobileImpl < cdmapc.IMobile
    
    properties (GetAccess = private, SetAccess = private)
        % Inicialmente 0 dBm
        power = 0;
        
        mobilityStrategy
        handoffManager
    end
    
    properties(GetAccess = public, SetAccess = private)
        powerControlStrategy
    end % properties
    
    methods (Access = public)
        
        %
        % Constructor
        %
        function [simpleMobileImpl] = SimpleMobileImpl(mobilityStrategy, powerControlStrategy, handoffManager)
            if(nargin == 0)
                error('Debe proporcionarse el algoritmo de movilidad para este dispositivo');
            elseif(nargin == 1)
                error('Debe proporcionarse el mecanismo de control de potencia para este dispositivo');
            elseif(nargin == 2)
                error('Debe proporcionarse el manejador de handoff para este dispositivo');
            elseif(~isa(mobilityStrategy, 'cdmapc.IMobilityStrategy'))
                error('El objeto mobilityStrategy debe ser de tipo IMobilityStrategy');
            elseif(~isa(powerControlStrategy, 'cdmapc.IPowerControlStrategy'))
                error('El objeto powerControlStrategy debe ser de tipo IPowerControlStrategy');
            elseif(~isa(handoffManager, 'cdmapc.IHandoffManager'))
                error('El objeto handoffManager debe ser de tipo IHandoffManager');
            end % if
            simpleMobileImpl.mobilityStrategy = mobilityStrategy;
            simpleMobileImpl.powerControlStrategy = powerControlStrategy;
            simpleMobileImpl.handoffManager = handoffManager;
            simpleMobileImpl.addListeners();
            simpleMobileImpl.move();
        end % function
        
        %
        % Destructor
        %
        function [] = delete(this)
            this.power = 0;
        end % function
        
        function [adjustedPower] = adjustPower(this, powerControlAdjustment)
            if(nargin == 1)
                error('Debe proporcionarse el objeto que encapsula el ajuste realizado durante el control de potencia');
            elseif(~isa(powerControlAdjustment, 'cdmapc.PowerControlAdjustment'))
                error('El objeto powerControlAdjustment debe ser de tipo PowerControlAdjustment');
            end % if
            adjustedPower = powerControlAdjustment.applyPowerAdjustment(this.power);
            this.power = adjustedPower;
        end % function
        
        function [newPosition] = move(this)
            newPosition = this.mobilityStrategy.move();
            this.currentPosition = newPosition;
        end % function
        
    end % methods
    
    methods (Access = private)
        
        function [] = addListeners(this)
            addlistener(this.powerControlStrategy, 'reverseLinkPowerControlPerformed', @this.handlePowerControlPerformed);
        end % function
        
        function [] = handlePowerControlPerformed(this, ~, eventData)
            this.adjustPower(eventData.powerControlAdjustment);
        end % function
        
    end % methods
    
end % classdef
